@extends('layouts.layouts')
@section('title')
    <title>后台首页</title>
@endsection
@section('content')
    <link rel="stylesheet" href="/lib/layer/theme/default/layer.css"/>
    <script type="text/javascript" src="/lib/layer/layer.js"></script>
    <div class="content">
        <div class="header">
            <ul class="breadcrumb">
                <li><a href="/special/index">专题管理</a></li>
                <li class="active">专题列表</li>
            </ul>
        </div>
        <div class="main-content">
            <div class="btn-toolbar list-toolbar">
                <form class="form-inline col-sm-4" action="" method="get">
                    <a href="/special/addSpecial"><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" ><i class="fa fa-plus"></i> 新增</button></a>
                    <!-- <div class="form-group">
                        <input type="text" class="form-control" name="title" placeholder="文章标题" value="">
                    </div>
                    <button type="submit" class="btn btn-default">筛选</button> -->
                </form>
               
            </div>

            <div class="row">
                <div class="col-sm-12 col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading no-collapse">文章列表</div>
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>编号</th>
                                    <th>专题名称</th>
                                    <th>封面图</th>
                                    <th>海报图片</th>
                                    <th>排序值</th>
                                    <th>更新时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                            @foreach($list as $k=>$v)
                                    <tr>
                                        <td>{{$v->id}}</td>
                                        <td><a href="/special/edit_special/{{$v->id}}">{{$v->title}}</a></td>
                                        <td><img src = "{{env('IMG_URL')}}{{$v->img_url}}" alt="" width="100px"/></td>
                                        <td><img src = "{{env('IMG_URL')}}{{$v->bill_url}}" alt="" width="100px"/></td>
                                        <td>{{$v->orderby}}</td>
                                        <td>{{$v->updated_at}}</td>
                                        <td> 
                                            <a class="btn btn-success btn-xs" href="/special/articleList/{{$v->id}}">查看详情</a>
                                        </td>
                                        <td> 
                                            <a class="btn btn-success btn-xs" href="/special/edit_special/{{$v->id}}">编辑</a>
                                            <button class="btn btn-danger btn-xs" data-id="{{$v->id}}" onclick="articleDelete(this)">删除</button>
                                            <button class="btn btn-info btn-xs" data-id="{{$v->id}}" data-state="{{$v->state}}" onclick="articleState(this)">{{$v->state?'下架':'上架'}}</button>
                                        </td>
                                    </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        
        </div>
    </div>
    <script type="text/javascript">
        //修改文章状态
        function articleState(obj){
            var id = $(obj).attr('data-id');
            var state = $(obj).attr('data-state');
            if(state == 1){
                var notice = '确定下架吗？';
                var newState = 0;
                var name = '上架'
            }else{
                var notice = '确定上架吗？';
                var newState = 1;
                var name = '下架';
            }
            var _token = '{{csrf_token()}}';
            var data = {id:id,state:newState,_token:_token};
            var object = obj;
            layer.confirm(notice, {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/special/changeState',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $(object).attr('data-state',newState);
                            $(object).text(name);
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });
        }

        //修改文章是否首页显示
        function articleIndex(obj){
            var id = $(obj).attr('data-id');
            var state = $(obj).attr('data-is_index');
            if(state == 1){
                var notice = '确定首页不显示吗？';
                var newState = 0;
                var name = '不显示'
            }else{
                var notice = '确定首页显示吗？';
                var newState = 1;
                var name = '首页显示';
            }
            var _token = '{{csrf_token()}}';
            var data = {id:id,state:newState,_token:_token};
            var object = obj;
            layer.confirm(notice, {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/article/changeindex',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $(object).attr('data-is_index',newState);
                            $(object).text(name);
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });
        }

        //修改文章是否精选推荐
        function articleSelected(obj){
            var id = $(obj).attr('data-id');
            var state = $(obj).attr('data-is_selected');
            if(state == 1){
                var notice = '确定不精选吗？';
                var newState = 0;
                var name = '不精选'
            }else{
                var notice = '确定精选吗？';
                var newState = 1;
                var name = '精选';
            }
            var _token = '{{csrf_token()}}';
            var data = {id:id,state:newState,_token:_token};
            var object = obj;
            layer.confirm(notice, {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/article/changeselected',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $(object).attr('data-is_selected',newState);
                            $(object).text(name);
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });
        }
        function articleDelete(obj){
            var id = $(obj).attr('data-id');
            var _token = '{{csrf_token()}}';
            var data = {id:id,_token:_token};
            layer.confirm('确定删除吗？', {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/special/delete',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $("#articleTr_"+id).remove();
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });

        }
    </script>
@endsection